<template>
  <el-breadcrumb class="breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item, index) in breadcrumbData" :key="item.path">
        <!-- 设置 noRedirect 和 最后一项时 为不可点击项 -->
        <span v-if="item.redirect === 'noRedirect' || index === breadcrumbData.length - 1" class="no-redirect">
          {{ item.meta.title }}
        </span>
        <!-- 可点击项 -->
        <router-link v-else class="redirect" :to="{ path: item.path }">{{ item.meta.title }}</router-link>
        <!-- <a v-else class="redirect" @click.prevent="onLinkClick(item)">{{ item.meta.title }}</a> -->
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script lang="ts" setup>
import { useGlobalStore } from '@/stores/global'

const route = useRoute()
const breadcrumbData = ref([])
const getBreadcrumbData = () => {
  // const firstItem: any[] = [{ path: '/', name: 'Home', meta: { title: '首页' } }]
  // firstItem.concat(breadcrumbData.value)
  breadcrumbData.value = route.matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
}

watch(route, () => getBreadcrumbData(), { immediate: true })

const global = useGlobalStore()
const linkHoverColor = ref(global.cssVar.menuBg)
</script>
<style lang="scss" scoped>
.breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 8px;

  .no-redirect {
    // color: #97a8be;
    cursor: text;
  }

  .redirect {
    // color: #666;
    font-weight: 600;
  }

  .redirect:hover {
    // 将来需要进行主题替换，所以这里不去写死样式
    color: v-bind(linkHoverColor);
  }
}
</style>
